package com.friend.joey.applicationfriend;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.baidu.trace.LBSTraceClient;
import com.baidu.trace.Trace;
import com.baidu.trace.api.track.HistoryTrackResponse;
import com.baidu.trace.api.track.OnTrackListener;
import com.baidu.trace.api.track.TrackPoint;
import com.baidu.trace.model.OnTraceListener;
import com.baidu.trace.model.PushMessage;
import com.friend.joey.applicationfriend.Classes.MyPoint;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Joey on 2017/5/17.
 */

public class TestSpeak extends AppCompatActivity{

    private Button ctrace;
    private Button check;
    private EditText checkResult;
    private MyHandler mHandler;
    private Trace mTrace;
    private LBSTraceClient mTraceClient;
    private OnTraceListener mTraceListener;
    private OnTrackListener mTrackListener;
    private List<TrackPoint> tracksList;
    private int total;
    private List<MyPoint> myPoint= new ArrayList<>();
    private TraceRecord traceRecord;

    private class MyHandler extends Handler {
        private WeakReference<TestSpeak> mWeakReference;
        public MyHandler(TestSpeak activity){
            mWeakReference=new WeakReference<TestSpeak>(activity);
        }
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            TestSpeak activity= mWeakReference.get();
            if(activity!=null){
                switch(msg.what){
                    case 3:
                        Toast.makeText(activity, msg.obj.toString(),Toast.LENGTH_SHORT).show();
                        break;
                    case 4:
                        Toast.makeText(activity, msg.obj.toString(),Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        Toast.makeText(activity, "没有信息", Toast.LENGTH_SHORT).show();
                }
            }
            else{
                Toast.makeText(TestSpeak.this, "没有Activity", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_speak );
        ctrace=(Button)findViewById(R.id.btn_cspeak);
        check=(Button)findViewById(R.id.btn_check);
        checkResult=(EditText) findViewById(R.id.checkresult);
        mHandler = new MyHandler(this);
        ////开启轨迹服务
        traceRecord=new TraceRecord("myTrace",getApplicationContext());
        traceRecord.setOnTraceListener(new TraceRecord.onTraceListener() {
            @Override
            public void onMBindServiceCallback(int i, String s) {

            }
            @Override
            public void onMStartTraceCallback(int status, String message) {
                Toast.makeText(TestSpeak.this,"开启服务:"+Integer.toString(status)+message.toString(), Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onMStopTraceCallback(int status, String message) {
                Toast.makeText(TestSpeak.this,"停止服务:"+Integer.toString(status)+message.toString(), Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onMStartGatherCallback(int status, String message) {
                Toast.makeText(TestSpeak.this,"开启采集:"+Integer.toString(status)+message.toString(), Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onMStopGatherCallback(int status, String message) {
                Toast.makeText(TestSpeak.this,"停止采集:"+Integer.toString(status)+message.toString(), Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onMPushCallback(byte messageNo, PushMessage message) {
                Toast.makeText(TestSpeak.this,"打包回传:"+message.toString(), Toast.LENGTH_SHORT).show();
            }
        });
        traceRecord.setOnTrackListener(new TraceRecord.onTrackListener() {
            @Override
            public void onNHistoryTrackCallback(HistoryTrackResponse response) {
                //Toast.makeText(TestSpeak.this,"查询轨迹:"+response.toString(), Toast.LENGTH_SHORT).show();
                checkResult.setText(response.toString());
                Toast.makeText(TestSpeak.this, Integer.toString(traceRecord.getTotal()), Toast.LENGTH_SHORT).show();
                Toast.makeText(TestSpeak.this, traceRecord.getTracksList().toString(), Toast.LENGTH_SHORT).show();
            }
        });
        traceRecord.startTrace();
        ////
        ctrace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 开启采集
                ///
                if(traceRecord.getServeState()) {
                    traceRecord.startTrace();
                }
                ///
            }
        });
        check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 停止服务
                ///
                if(traceRecord.getServeState()) {
                    traceRecord.stopTrace();
                    //traceRecord.stopService();
                    traceRecord.startCheck();
                }
                ///
            }
        });

        Button ShowTrace=(Button)findViewById(R.id.btn_showtrace);
        ShowTrace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.SHOWTRACE");
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(traceRecord.getServeState()) {
            traceRecord.stopTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(traceRecord.getServeState()) {
            traceRecord.stopTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(traceRecord.getServeState()) {
            traceRecord.stopTrace();
        }
    }
}
